package com.zhenmaitang.clinic_sys.service.impl;

import com.zhenmaitang.clinic_sys.entity.Role;
import com.zhenmaitang.clinic_sys.entity.UserRole;
import com.zhenmaitang.clinic_sys.service.RoleService;
import com.zhenmaitang.clinic_sys.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    
    // 模拟数据库存储
    private final Map<Integer, Role> roles = new HashMap<>();
    private Integer nextId = 1;
    
    @Autowired
    private UserRoleService userRoleService;

    public RoleServiceImpl() {
        // 初始化默认角色
        Role adminRole = new Role("ADMIN", "系统管理员，可以查看所有数据", 
            "[\"USER_READ\",\"USER_CREATE\",\"USER_UPDATE\",\"USER_DELETE\",\"DEPARTMENT_READ\",\"DEPARTMENT_CREATE\",\"DEPARTMENT_UPDATE\",\"DEPARTMENT_DELETE\",\"ROLE_READ\",\"ROLE_CREATE\",\"ROLE_UPDATE\",\"ROLE_DELETE\",\"FINANCE_READ\",\"FINANCE_UPDATE\",\"HR_READ\",\"HR_UPDATE\"]");
        adminRole.setId(nextId++);
        roles.put(adminRole.getId(), adminRole);

        Role financeRole = new Role("FINANCE", "财务部用户，只能查看财务部数据", 
            "[\"FINANCE_READ\",\"FINANCE_UPDATE\"]");
        financeRole.setId(nextId++);
        roles.put(financeRole.getId(), financeRole);

        Role hrRole = new Role("HR", "人事部用户，只能查看人事部数据", 
            "[\"HR_READ\",\"HR_UPDATE\"]");
        hrRole.setId(nextId++);
        roles.put(hrRole.getId(), hrRole);
    }

    @Override
    public List<Role> getAllRoles() {
        return new ArrayList<>(roles.values());
    }

    @Override
    public Role getRoleById(Integer id) {
        return roles.get(id);
    }

    @Override
    public Role getRoleByName(String name) {
        return roles.values().stream()
                .filter(role -> role.getName().equals(name))
                .findFirst()
                .orElse(null);
    }

    @Override
    public Role createRole(Role role) {
        role.setId(nextId++);
        roles.put(role.getId(), role);
        return role;
    }

    @Override
    public Role updateRole(Role role) {
        if (role.getId() != null && roles.containsKey(role.getId())) {
            roles.put(role.getId(), role);
            return role;
        }
        throw new RuntimeException("角色不存在");
    }

    @Override
    public void deleteRole(Integer id) {
        roles.remove(id);
    }

    @Override
    public List<Role> getRolesByUserId(Integer userId) {
        // 通过user_roles关联表查询用户的角色
        List<UserRole> userRoles = userRoleService.getUserRolesByUserId(userId);
        List<Role> userRoleList = new ArrayList<>();
        
        // 根据角色ID获取角色信息
        for (UserRole userRole : userRoles) {
            Role role = getRoleById(userRole.getRoleId());
            if (role != null) {
                userRoleList.add(role);
            }
        }
        
        return userRoleList;
    }
}